from selenium import webdriver
from pytesseract import *
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
import time
from time import ctime,sleep
import unittest

#****=======******=========*******=========*********=========****========***=


driver=webdriver.Chrome(r'D:\Python\chromedriver.exe')
url="https://www.baidu.com"
url1="http://211.149.162.130:8044/#/login"
driver.get(url1)


#****=======******=========*******=========*********=========****========***=

#id进行定位
# driver.find_element_by_id("kw").send_keys("TEST")
# driver.find_element_by_id("su").click()

#Xpath定位
#driver.find_element_by_xpath("//*[@id='u1']/a[5]").click()

#class 进行定位bg s_btn
# driver.find_element_by_class_name("s_ipt").send_keys("Python")
# driver.find_element_by_class_name("bg s_btn").click()

#****=======******=========*******=========*********=========****========***=

#
#设置浏览器的打开窗体大小
#driver.set_window_size(480,800)

#全屏打开
driver.maximize_window()


#****=======******=========*******=========*********=========****========***=

#控制浏览器前进和后退

# first_url='https://tieba.baidu.com/index.html'
# print("进入贴吧页面 %s" %(first_url))
# driver.get(first_url)
# time.sleep(1)
# print("返回上一级的页面 %s" %(url))
# driver.back()
# time.sleep(1)
# print("前进到上一个页面 %s" %(first_url))
# driver.forward()
# print("回到主界面")
# driver.get(url)

#****=======******=========*******=========*********=========****========***=

# #模拟浏览器刷新
# driver.find_element_by_id("kw").send_keys("准备测试刷新")
# driver.refresh()
# driver.find_element_by_id("kw").send_keys("准备测试清除文本")
# time.sleep(2)
# #清除文本
# driver.find_element_by_id("kw").clear()

#****=======******=========*******=========*********=========****========***=

# #鼠标点击操作 click为左点击。 context_click为右点击
# left_click=driver.find_element_by_css_selector("div.head_nums_cont_outer.OP_LOG>div>div.nums>div")
# ActionChains(driver).click(left_click).perform()
#
# left_click_time=driver.find_element_by_xpath("//*[@id='container']/div[2]/div/div[1]/span[2]")
# ActionChains(driver).click(left_click_time).perform()
#
#****=======******=========*******=========*********=========****========***=

# #悬浮鼠标在设置按钮上  **********************未成功
# fire_set=driver.find_element_by_xpath("//*[@id='container']/div[2]/div/div[1]/span[2]")
# ActionChains(driver).move_to_element(fire_set).perform()



#ActionChains(driver).move_to_element(left_click).perform()


#****=======******=========*******=========*********=========****========***=


#键盘按键测试
# driver.find_element_by_id("kw").send_keys("键盘测试")
# driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')
# driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'c')
# for i in range(2):
#     driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'v')

#****=======******=========*******=========*********=========****========***=

#百度账号登录测试
# driver.find_element_by_xpath("//*[@id='u1']/a[6]").click()
# print(driver.title)
# driver.find_element_by_link_text("短信快捷登录").click()
# time.sleep(2)
# driver.find_element_by_xpath("//*[@id='TANGRAM__PSP_3__smsPhone']").send_keys("17828479011")
# time.sleep(2)
# driver.find_element_by_xpath("//*[@id='TANGRAM__PSP_3__smsTimer']").click()

#****=======******=========*******=========*********=========****========***=

#显示等待测试
# element=WebDriverWait(driver,5,0.5).until(EC.presence_of_element_located((By.ID,"kw")))
# driver.quit()

#****=======******=========*******=========*********=========****========***=
#隐式等待
#在js加载时候 定位一个元素，若定位不到

# driver.implicitly_wait(10)  #设置隐式等待时间为10秒
# try:
#     print(ctime())
#     driver.find_element_by_id("kw22").send_keys("Python")  #百度的主页没有 id=kw22的 so 在轮询机制后没有定位到该元素，隐式等待10s
# except NoSuchElementException as e:
#     print(ctime())
#     print(e)
# finally:
#     print(ctime())


 #****=======******=========*******=========*********=========****========***=

#sleep休眠

# sleep(10)
# #休眠10秒

#****=======******=========*******=========*********=========****========***=

# 多列表切换
# 有表单镶嵌页面的时候 iframe 和 frame 不能在本页面上的html上对 表单中出现的网页元素进行定位
# 如：<iframe id="if" name="if" src="https://www.baidu.com" widh="800" height="300">  </iframe>
  #切换到iframe(id="if")中

# driver.switch_to.frame("if")

#当frame或者iframe没有id存在的时候
#xf=driver.find_element_by_xpath("xpath路径")
#driver.switch_to.frame(xf)


#****=======******=========*******=========*********=========****========***=

#多窗口切换

# driver.implicitly_wait(10)
# #获得当前打开页面的句柄
# sreach_windows=driver.current_window_handle
#
# driver.find_element_by_link_text('登录').click()
# driver.find_element_by_link_text('立即注册').click()
#
# #获得所有打开窗口的句柄
#
# sreach_all_windows=driver.window_handles
# print(type(sreach_windows))
# print(type(sreach_all_windows))
#
#
# for handle in sreach_all_windows:
#     if handle !=sreach_windows:
#         driver.switch_to_window(handle)
#         print("welcome to input windows")
#         driver.find_element_by_id("TANGRAM__PSP_3__userName").send_keys("测试呀")
#         time.sleep(5)
#
# for handle in sreach_all_windows:
#     if handle == sreach_windows:
#         driver.switch_to_window(sreach_windows)
#         print("back to windows")
#         driver.back()
#         driver.find_element_by_id("kw").send_keys("完成")
#         time.sleep(2)
#
# for handle in sreach_all_windows:
#     print(handle)

#*****************=======******=========*******=========*********=========****========***=


#刷新

driver.refresh()

#****=======******=========*******=========*********=========****========***=======******

#有隐藏元素不可见，so                         *=*=*=*=*=*=*=*==*=*=*=*=*=*==*=*=*=*=*=*==* 等待解决

# link=driver.find_element_by_link_text("设置")
# ActionChains(driver).move_to_element(link).perform()
#
# driver.find_element_by_link_text("搜索设置").click()
# print(driver.current_url)
#
#
# driver.find_element_by_class_name("prefpanelgo").click()




# sleep(10)


# windows1=driver.window_handles
# print(windows1)


#****=======******=========*******=========*********=========****========***=======******

#窗口截图

# driver.get_screenshot_as_file("D:\\test\\baidu.jpg")

#****=======******=========*******=========*********=========****========***=======******

#Cookie 操作

# #获取cookies
# cookie=driver.get_cookies()
# print(cookie)
# for i in cookie:
#     print(i)
#
# #添加cookie
#
# driver.add_cookie({'name':'xuhaoya','value':'xiaoxiannvya'})
#
# test_cookie=driver.get_cookie('name')
# print(test_cookie)


#****=======******=========*******=========*********=========****========***=======******

#python验证码的识别

# auth_code=driver.find_element_by_xpath()


#****=======******=========*******=========*********=========****========***=======******

#模块化测试  unittest







#****=======******=========*******=========*********=========****========***=======******

driver.find_element_by_xpath("//*[@id='app']/div/form/div[1]/div/div[1]/input").send_keys("admin")
driver.find_element_by_xpath("//*[@id='app']/div/form/div[2]/div/div/input").send_keys("123456")
driver.find_element_by_xpath("//*[@id='app']/div/form/div[3]/div/button").click()



sleep(1)

driver.refresh()

driver.find_element_by_xpath("//*[@id='app']/div/div[2]/aside/ul/li[6]/div").click()
driver.find_element_by_xpath("//*[@id='app']/div/div[2]/aside/ul/li[6]/ul/li[1]").click()

def FormTable():
    driver.find_element_by_xpath("//*[@id='app']/div/div[2]/section/div/div[2]/section/form/div/div[1]/div[1]/div/div/input").send_keys("测试")
    driver.find_element_by_xpath("//*[@id='app']/div/div[2]/section/div/div[2]/section/form/div/div[1]/div[5]/div/div/input").send_keys("15520")
    driver.find_element_by_xpath("//*[@id='app']/div/div[2]/section/div/div[2]/section/form/div/div[1]/div[10]/div/div/input").send_keys("小区")
    driver.find_element_by_xpath("//*[@id='app']/div/div[2]/section/div/div[2]/section/form/div/div[1]/div[11]/div/div/input").send_keys("1-210")
    sleep(1)
    driver.find_element_by_xpath("//*[@id='app']/div/div[2]/section/div/div[2]/section/form/div/div[1]/div[4]/div/div/div[1]/input").click()
    sleep(1)
    x1=driver.find_element_by_xpath("/html/body/div[2]/div/div[1]/ul/li[1]")
    ActionChains(driver).click(x1).perform()

    sleep(1)
    driver.find_element_by_xpath("//*[@id='app']/div/div[2]/section/div/div[2]/section/form/div/div[1]/div[8]/div/div/div[1]/input").click()
    sleep(1)
    x3 = driver.find_element_by_xpath("/html/body/div[3]/div/div[1]/ul/li[1]")
    ActionChains(driver).click(x3).perform()

    sleep(1)
    driver.find_element_by_xpath(
        "//*[@id='app']/div/div[2]/section/div/div[2]/section/form/div/div[1]/div[9]/div/div/div[1]/input").click()
    sleep(1)
    x2 = driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/ul/li[1]")
    ActionChains(driver).click(x2).perform()

    sleep(1)
    driver.find_element_by_xpath(
        "//*[@id='app']/div/div[2]/section/div/div[2]/section/form/div/div[1]/div[12]/div/div/div[1]/input").click()
    sleep(1)
    x2 = driver.find_element_by_xpath("/html/body/div[5]/div/div[1]/ul/li[1]")
    ActionChains(driver).click(x2).perform()

    sleep(1)
    driver.find_element_by_xpath("//*[@id='app']/div/div[2]/section/div/div[2]/section/form/div/div[1]/div[6]/div/div/div[2]/input").click()
    sleep(1)
    x2 = driver.find_element_by_xpath("/html/body/div[6]/div/div[1]/ul/li[1]")
    ActionChains(driver).click(x2).perform()

    driver.find_element_by_xpath("//*[@id='app']/div/div[2]/section/div/div[2]/section/form/div/div[1]/div[14]/div/div/button").click()


for i in range(20):
    sleep(1)
    FormTable()
    driver.refresh()

    driver.find_element_by_xpath("//*[@id='app']/div/div[2]/aside/ul/li[6]/div").click()
    driver.find_element_by_xpath("//*[@id='app']/div/div[2]/aside/ul/li[6]/ul/li[1]").click()


# driver.switch_to_alert().accept()



# driver.close()
#
# driver=webdriver.Chrome(r'D:\Python\chromedriver.exe')
# driver.get("http://211.149.162.130:8555/#/login")
#
# driver.find_element_by_xpath("//*[@id='app']/div/form/div[1]/div/div[1]/input").send_keys("00015")
# driver.find_element_by_xpath("//*[@id='app']/div/form/div[2]/div/div[1]/input").send_keys("123456")
# driver.find_element_by_xpath("//*[@id='app']/div/form/div[3]/div/button").click()
# driver.find_element_by_xpath("//*[@id='app']/div/div[1]/div[2]/div/a[2]/i").click()

